//
//  BCIndustryCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/19.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCIndustryTopCell.h"

//
#import "HomeCellDisplayCollectionView.h"
#import "HomeCellDataView.h"

//
#import "CustomProjectCountDownView.h"

@interface BCIndustryTopCell ()

@property (nonatomic, strong) UILabel *titleLabel;

/**
 *displayCollection
 */
@property (nonatomic, strong) HomeCellDisplayCollectionView *displayCollection;

/**
 *rateCaptionLabel
 */
@property (nonatomic, strong) UILabel *rateCaptionLabel;

/**
 *rateLabel
 */
@property (nonatomic, strong) UILabel *rateLabel;

/**
 *HomeCellDataView
 */
@property (nonatomic, strong) HomeCellDataView *totleDataView;

/**
 *HomeCellDataView
 */
@property (nonatomic, strong) HomeCellDataView *lastDataView;

/**
 *CustomProjectCountDownView
 */
@property (nonatomic, strong) CustomProjectCountDownView *countdownView;


/**
 *button
 */
@property (nonatomic, strong) QMUIButton *button;


@end

@implementation BCIndustryTopCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.titleLabel = [UILabel new];
    self.titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.titleLabel.numberOfLines = 2;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:19 weight:UIFontWeightMedium];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(self.contentView).offset(-40.0);
        make.bottom.mas_equalTo(self.contentView.mas_top).offset(35.0+self.titleLabel.font.lineHeight);
    }];
    //
    self.displayCollection = [[HomeCellDisplayCollectionView alloc] initWithFrame:CGRectMake(20, 0, ScreenWidth-40, 0) collectionViewLayout:[UICollectionViewFlowLayout new]];
    [self.contentView addSubview:self.displayCollection];
    [self.displayCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(8.0);
        make.centerX.mas_equalTo(self.contentView);
        make.height.mas_equalTo([HomeCellDisplayCollectionView defaultHeight]);
        make.width.mas_equalTo(200.0);
    }];
    
    self.rateCaptionLabel = [UILabel new];
    self.rateCaptionLabel.textAlignment = NSTextAlignmentCenter;
    self.rateCaptionLabel.font = [UIFont systemFontOfSize:11 weight:UIFontWeightRegular];
    self.rateCaptionLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.rateCaptionLabel.adjustsFontSizeToFitWidth = true;
    [self.contentView addSubview:self.rateCaptionLabel];
    [self.rateCaptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(47.0);
        make.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView).offset(-40.0);
        make.height.mas_equalTo(self.rateCaptionLabel.font.lineHeight+1);
    }];
    //
    self.rateLabel = [UILabel new];
    self.rateLabel.textAlignment = NSTextAlignmentCenter;
    self.rateLabel.font = [UIFont systemFontOfSize:23 weight:UIFontWeightMedium];
    self.rateLabel.textColor = [UIColor darkMainColor];
    [self.contentView addSubview:self.rateLabel];
    [self.rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.rateCaptionLabel.mas_bottom).offset(10.0);
        make.height.mas_equalTo(self.rateLabel.font.lineHeight+1);
    }];
    //
    self.totleDataView = [HomeCellDataView new];
    [self.contentView addSubview:self.totleDataView];
    [self.totleDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.rateLabel.mas_bottom).offset(20.0);
        make.left.mas_equalTo(self).offset(25.0);
        make.right.mas_equalTo(self.mas_centerX).offset(-5.0);
        make.height.mas_equalTo([HomeCellDataView defaultHeight]);
    }];
    self.lastDataView = [HomeCellDataView new];
    [self.contentView addSubview:self.lastDataView];
    [self.lastDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.totleDataView);
        make.left.mas_equalTo(self.mas_centerX).offset(25.0);
        make.right.mas_equalTo(self).offset(-25.0);
        make.height.mas_equalTo([HomeCellDataView defaultHeight]);
    }];
    //
    CGFloat countdownDefaultHeight = [CustomProjectCountDownView defaultHeight];
    self.countdownView = [CustomProjectCountDownView new];
    [self.contentView addSubview:self.countdownView];
    [self.countdownView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView).offset(-46.0);
        make.centerY.mas_equalTo(self.totleDataView.mas_bottom).offset(10.0+countdownDefaultHeight/2);
        make.height.mas_equalTo(countdownDefaultHeight);
    }];
    //
    UIView *buttonPreferenceView = [UIView new];
    [self.contentView addSubview:buttonPreferenceView];
    [buttonPreferenceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.countdownView.mas_bottom);
        make.bottom.mas_equalTo(self.contentView);
        make.left.right.mas_equalTo(self.contentView);
    }];
    CGSize buttonSize = CGSizeMake(280, 45);
    self.button = [QMUIButton new];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.button.backgroundColor = [UIColor darkMainColor];
    self.button.layer.cornerRadius = buttonSize.height/2;
    [self.contentView addSubview:self.button];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(buttonPreferenceView);
        make.centerX.mas_equalTo(self.contentView);
        make.size.mas_equalTo(buttonSize);
    }];
    [self.button addTarget:self action:@selector(buttonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
}



- (void)setCellData:(BCProjectModel *)cellData {
    _cellData = cellData;
    //
    self.titleLabel.text = cellData.project_name;
    //
    self.rateCaptionLabel.text = cellData.repayCaption;
    self.rateLabel.text = cellData.repay;
    
     [self.button setTitle:cellData.not_buy forState:UIControlStateNormal];
    
    //
    self.totleDataView.title = cellData.target_title;
    self.totleDataView.data = cellData.target;
    self.lastDataView.title = cellData.surplus_title;
    self.lastDataView.data = cellData.surplus;
    
    self.displayCollection.displayArray = [cellData.tags componentsSeparatedByString:@","];
    
    self.countdownView.timeConsumingString = cellData.taking;
    [self.countdownView setCountdownString:cellData.countdownMess endTime:cellData.end_time];
    
    [self.displayCollection reloadData];
    [self layoutDisplayCollectionView];
}

//layoutcollection让其居中
- (void)layoutDisplayCollectionView {
    CGFloat maxWidth = ScreenWidth-40.0;
    CGFloat contentSizeWidth = self.displayCollection.contentSize.width;
    CGFloat width = MIN(maxWidth, contentSizeWidth);
    [self.displayCollection mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self layoutDisplayCollectionView];
}


#pragma mark - buttonDidClickedAction
- (void)buttonDidClickedAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(industryTopCellDidClickedBuyButton:)]) {
        [self.delegate industryTopCellDidClickedBuyButton:self.cellData];
    }
}




+ (CGFloat)defaultHeight {
    return 340.0;
}

@end
